<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use DB;
use Markdown;
use App\Model\Art;
use Auth;
use Gregwar\Captcha\CaptchaBuilder;
use Validator;
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $arts = DB::table('arts')->orderBy('id','desc')->paginate(3);
        // $arts = Art::all();
        //ORM
        $arts = Art::orderBy('id','desc')->paginate(5);
        // return $arts;
        // return view('art.index',$arts);
        return view('art.index',['arts'=>$arts]);
        // return $arts;
        // $str="## h1"; 
        // echo Markdown::convertToHtml($str); 
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("art.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $data = $request->except('_token');
        
        // $title = $request->title;
        // $content = $request->content;
        // $res = DB::table('arts')->insert(['title'=>$title,'content'=>$content]);
        // if($res){
        //     return 1;
        // }else{
        //     return back();
        // }
        
        //2.caeate 方法
        // $res = Art::create(['title'=>$request->title,'content'=>$request->content]);

        //3.save方法


        //字段验证
        //门面validate的验证方式
        $val =  Validator::make($request->all(), [
        'title' => 'required|max:30|min:8',
        'content' => 'required|max:1000|min:10',
         ]);
        //ajax会有问题
        //没有通过验证
        if($val->fails()){
            $r['error'] = 0;
            return $r;
            // return back()->withErrors($val)->withInput();
        }else{

            $art = new Art();
            $art->title = $request->title;
            $art->content = $request->content;
            // return dd($art);
            $res = $art->save();
            if($res){
                 $r['error'] = 1;
            }else{
                 $r['error'] = 0;
            }
            return $r;//直接返回就是json数据
        }  



        // $this->validate($request,[
        //     'title' => 'required|max:30|min:8',
        //     'content' => 'required|max:1000|min:50',
        // ],[
        //     'required' => ':attribute 是必填的',
        // ]);

        // $art = new Art();
        // $art->title = $request->title;
        // $art->content = $request->content;
        // // return dd($art);
        // $res = $art->save();
        // if($res){
        //      $r['error'] = 1;
        // }else{
        //      $r['error'] = 0;
        // }
        // return $r;//直接返回就是json数据



    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // $art = DB::table('arts')->where('id','=',$id)->first();
        // $art->content= Markdown::convertToHtml($art->content);
        // dd($art);
        $art = Art::find($id);
        return view('art.show',['art'=>$art]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // $art = DB::table('arts')->where('id',$id)->first();
        $art = Art::find($id);

        return view('art.edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // $res = DB::table('arts')->where('id',$id)
        //    ->update(['title'=>$request->title,'content'=>$request->content]);

        //第一种方法
        // $art = Art::find($id);
        // $art->title = $request->title;
        // $art->content = $request->content;
        // $art->save();

        //第二种where更新
        Art::where('id', $id)
            ->update($request->except('_token','_method'));
        return redirect('/art');
    }

    /**
     * Remove the specified resource from storage.
     *删除 DELETE方法提交
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // $res = DB::table('arts')->where('id',$id)->delete();
        // Art::destroy($id);
        // return redirect('/art');
         $art = Task::find($id);
        $art->delete();
        return response()->json(['success']);
    }

    //传输数据
    public function dis(){
        $arts = Art::orderBy('id','desc')->paginate(3);
        return $arts;
    }

    //ajax删除
    public function delete(Request $request){
        $id = $request['id'];
        // $id = Input::get('name');
        // dd($id);
        return Art::where('id',$id)->delete();
        // if(Art::where('id',$id)->delete()){
        //     $data['error'] = 1;
        // }else{
        //     $data['error'] = 0;
        // }
        // return json_encode($data);  
        // return redirect('/art');
       
    }

    public  function  getUser()
    {
        //获取用户信息
        dd(Auth::user());
        //验证用户 
        // dd(Auth::check());
    }

    //退出登陆
    public function getLogout()
    {
         Auth::logout();   
    }

    //手动认证用户,你也可以不使用 Laravel 自带的认证控制器。
    public  function postLogin(Request $request)
    {
        $res = Auth::attempt(['email' => $request->email, 'password' => $request->password]);
        if($res){
           return redirect('/');
        }else{
           return back();
        }
    }

    //生成验证码
    public function getCode()
    {
        $builder = new CaptchaBuilder;
        $builder->build(120,35,null);
        //获取验证码中的内容
        $data=$builder->getPhrase();
        // dd($data);  
        //把验证码的内容闪存到session里面
        session()->flash('mycode',$data);
        // dd(session());
        // return response($builder->output())->header('Content-type','image/jpeg');
        return response($builder->output())->header('Content-type','image/jpeg');
    }

}
